<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>v-for</title>
    <script type="text/javascript" src="../assets/js/vue.js"></script>
</head>
<body>
    <h1>v-for</h1>
    <hr>
    <div id="app">
        <ul>
            <li v-for="item in sortItem">{{item}}</li>
        </ul>
        <hr>
        <ul>
            <li v-for="(studen, index) in sortStudens">{{index + 1}}，{{studen.name}}-{{studen.age}}</li>
        </ul>
    </div>
    <script>
        var app = new Vue({
            el : "#app",
            data : {
                items : [52, 24, 87, 65, 10, 5],
                studens : [
                    {name:'o', age : 25},
                    {name:'a', age : 23},
                    {name:'f', age : 10},
                    {name:'g', age : 30},
                ]
            },
            computed : {
                sortItem : function(){
                    return this.items.sort((a,b) => a-b);
                },
                sortStudens : function(){
                    return sortByKey(this.studens, "age");
                }
            }
        })

        //数组对象方法排序:
        function sortByKey(array,key){
            return array.sort(function(a,b){
            var x=a[key];
            var y=b[key];
            return ((x<y)?-1:((x>y)?1:0));
        });
        }
    </script>
</body>
</html>